//Floyd
#include <cstdio>

int map[21][21];

int main(int argc, char const* argv[])
{
    int num;
    int set_count = 0;
    while (scanf("%d", &num) != EOF) {
        for (int i = 0; i < 21; i++) {
            for (int j = 0; j < 21; j++) {
                map[i][j] = 0;
            }
        }
        int id;
        for (int j = 0; j < num; j++) {
            scanf("%d", &id);
            map[1][id] = 1;
            map[id][1] = 1;
        }
        for (int i = 2; i <= 19; i++) {
            scanf("%d", &num);
            for (int j = 0; j < num; j++) {
                scanf("%d", &id);
                map[i][id] = 1;
                map[id][i] = 1;
            }
        }
        //Floyd
        for (int i = 1; i <= 20; i++) {
            for (int j = 1; j <= 20; j++) {
                for (int k = 1; k <= 20; k++) {
                    if(map[k][i] == 0 || map[i][j] == 0)
                        continue;
                    if(map[k][j] == 0 || map[k][i] + map[i][j] < map[k][j])
                        map[k][j] = map[k][i] + map[i][j];
                }
            }
        }
        printf("Test Set #%d\n", ++set_count);
        int case_num;
        scanf("%d", &case_num);
        for (int i = 0; i < case_num; i++) {
            int start, end;
            scanf("%d%d", &start, &end);
            printf("%d to %d: %d\n", start, end, map[start][end]);
        }
        printf("\n");
    }
    return 0;
}